package engin

import (
	"context"

	"gitee.com/saroeleven/ginrcpv2/etcdmgr/registry"
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
)

var etcdRegistry = registry.NewEtcdRegistry()

type Engine struct {
	*gin.Engine
}

// create gin.engine
func New() *Engine {
	gin.SetMode(gin.ReleaseMode)
	e := &Engine{
		Engine: gin.New(),
	}
	e.Use(gin.Recovery())
	pprof.Register(e)
	return e
}

// run
func (e *Engine) DefRun(svc *registry.Service) error {
	if err := etcdRegistry.Register(context.TODO(), svc); err != nil {
		return err
	}
	return e.Engine.Run(svc.Addr)
}
